package inet.app.vod.adapter;

import inet.app.vod.activity.R;
import inet.app.vod.listeners.SlideImageClickListener;
import inet.app.vod.object.SlideObject;

import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.inet.app.framework.bitmap.UniversalImageUtil;

public class SlideImagesAdapter extends PagerAdapter {

	private Context mContext;
	private List<SlideObject> mSlideModels;
	private Boolean isShowTitle;
	private SlideImageClickListener onSlideClickListener;

	public SlideImagesAdapter(Context context, List<SlideObject> models,
			SlideImageClickListener listener) {
		mContext = context;
		mSlideModels = models;
		isShowTitle = true;
		onSlideClickListener = listener;
	}

	public void showTitle(Boolean isShowTitle) {
		this.isShowTitle = isShowTitle;
	}

	@Override
	public int getCount() {
		if (mSlideModels == null) {
			return 0;
		}
		return mSlideModels.size();
	}

	@Override
	public Object instantiateItem(View collection, final int position) {
		LayoutInflater inflater = (LayoutInflater) collection.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View view = inflater.inflate(R.layout.item_viewpager_word_images, null);
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (onSlideClickListener != null) {
					onSlideClickListener.onSlideClick(mSlideModels
							.get(position));
				}
			}
		});

		final SlideObject model = mSlideModels.get(position);

		ImageView imgView = (ImageView) view.findViewById(R.id.slide_img);
		UniversalImageUtil.getInstance(mContext).displayImage(model.getImg(),
				imgView);
		TextView title = (TextView) view.findViewById(R.id.title_text);
		title.setText(model.getTitle());
		title.setVisibility(isShowTitle ? View.VISIBLE : View.GONE);
		((ViewPager) collection).addView(view, 0);
		return view;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);

	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

}